package com.hex.ds.hdrs.period.task.po;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.hex.bigdata.hbda.utils.StringUtils;
import com.hex.ds.hdrs.common.utils.SecToTimeUtil;
import com.hex.ds.hdrs.period.job.po.PeriodJob;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Data
@Accessors(chain = true)
public class PeriodTask implements Serializable {

    /**
     * 任务延时后可执行时间
     */
    private long delayTs;

    /**
     * 系统重置任务次数
     */
    private int sysResetCount;

    /**
     * 任务运转轨迹<type,TaskTrack>
     */
    private Map<String, TaskTrack> taskTrackMap = new HashMap<>();

    /**
     * 任务运转轨迹, 按时间排序<type,TaskTrack>
     */
    private List<TaskTrack> taskTrackList = new ArrayList<>();

    private String pkId;

    private String jobCode;

    private String taskCode;

    private String taskName;

    private String bizDate;

    private String shellPath;

    private String taskStatus;

    private int errCount;

    private String crtTs;

    private String upTs;

    private String jobPkId;

    private String isEndTask;

    private String mutexJob;

    private String mutexTask;

    private String isOrder;

    private String batchTag;

    private int allowErrCount;

    private Integer resNum;

    private Integer priorityLevel;

    private String appCode;

    private String agentCode;

    private String appointAgentCode;

    private String planExecDuration;

    private String overPlanDuration;

    private String errDifferDuration;

    private String isOneAgent;

    private String execStTs;

    private String execEndTs;

    private String dependJob;

    private String dependTask;

    private String taskExecLog;

    private String extParam;

    private String trackJson;

    private String mapKey;

    /**
     * 任务延时后可执行时间(字符)
     */
    private String delayTsStr;

    private String costTs;

    private String taskRemark;

    private Integer offsetHour;

    private String jobName;

    private Integer taskStatusCount;

    private List<String> jobCodeList;

    private List<String> taskCodeList;

    private List<String> taskNameList;

    private String execStTsSt;

    private String execStTsEnd;

    private String startBz;

    private String endBz;

    private String execEndTsSt;

    private String execEndTsEnd;

    private String jobCodeFuzzy;

    private String taskCodeFuzzy;

    private String taskNameFuzzy;

    private List<String> pkIds;

    @Override
    public int hashCode() {
        int result = getTaskCode() != null ? getTaskCode().hashCode() : 0;
        result = result + getBizDate().hashCode();
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PeriodTask periodTask = (PeriodTask) o;
        return StrUtil.equalsAnyIgnoreCase(this.getTaskCode(), periodTask.getTaskCode())
                && StrUtil.equalsAnyIgnoreCase(this.getBizDate(), periodTask.getBizDate());
    }

    public String getTaskCodeAndName() {
        return taskCode + "+" + taskName;
    }

    public List<TaskTrack> getTaskTrackList() {
        if (CollectionUtil.isEmpty(taskTrackMap)){
            return taskTrackList;
        }
        return taskTrackMap.values().stream().sorted(Comparator.comparing(TaskTrack::getUpTs)).collect(Collectors.toList());
    }

    public String getCostTs() {
        if (StrUtil.isNotBlank(execStTs) && StrUtil.isNotBlank(execEndTs)) {
            return SecToTimeUtil.getCostTs(execStTs, execEndTs);
        }
        return costTs;
    }
}